<?php
namespace app\shop\model;

use app\BaseModel;
use app\farm\model\Claim;
use app\farm\model\LandList;
use app\farm\model\ShopGoods;
use app\farm\model\User;
use app\farm\server\Land;
use think\facade\Db;

class DistributionCash extends BaseModel
{



    protected $name = 'lbfarm_v2_distribution_cash_list';

    protected $append = [

        'order_goods'
    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:商品详情
     * @author chenniang
     * @DataTime: 2022-07-27 17:44
     */
    public function getOrderGoodsAttr($value,$data){

        if(!empty($data['type'])&&!empty($data['id'])){

            $goods_model = new DistributionGoods();

            if($data['type']==1){

                $list = $goods_model->getShopGoods($data['id']);

            }elseif($data['type']==2){

                $list = $goods_model->getLandGoods($data['id']);

            }else{

                $list = $goods_model->getCliamGoods($data['id']);

            }

            return $list;

        }

    }



    /**
     * @param $type
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-21 17:02
     */
    public function returnType($type){

        switch ($type){

            case 1:

                $text = '商城产品';

                break;
            case 2:

                $text = '土地产品';

                break;
            case 3:

                $text = '认养产品';

                break;
            default:

                $text = '商城产品';

                break;

        }

        return $text;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @param $order
     * @param $type
     * @param int $status
     * @功能说明:添加商城分销记录
     * @author chenniang
     * @DataTime: 2022-07-27 13:51
     */
    public function addUserCashShop($fx_id,$top_id,$order,$type,$status=1){


        if(!empty($order['order_goods'])){

            $goods_model = new ShopGoods();

            $cash_goods_model = new DistributionGoods();

            $is_fx = $total_cash = 0;

            foreach ($order['order_goods'] as &$v){

                $goods_info = $goods_model->dataInfo(['id'=>$v['goods_id']]);

                if(!empty($goods_info['is_fx'])){

                    $v['fx_balance'] = $type==1?$goods_info['one_fx']:$goods_info['two_fx'];

                    $v['is_fx']  = $goods_info['is_fx'];

                    $v['fx_cash']= $v['pay_price']*$v['fx_balance']/100;

                    $total_cash  += $v['fx_cash'];

                    $is_fx = 1;

                }
            }
            //说明有分销商品
            if($is_fx==1){

                $insert = [

                    'uniacid'   => $order['uniacid'],

                    'user_id'   => $top_id,

                    'source_id' => $order['user_id'],

                    'order_code'=> $order['order_code'],

                    'transaction_id'=> $order['transaction_id'],

                    'order_id' => $order['id'],

                    'type'     => 1,

                    'status'   => $status,

                    'fx_type'  => $type,

                    'cash'     => round($total_cash,2),

                    'reseller_id'=> $fx_id,
                ];

                $this->dataAdd($insert);

                $id = $this->getLastInsID();

                foreach ($order['order_goods'] as $v){

                    if($v['is_fx']==1){

                        $insert = [

                            'uniacid' => $order['uniacid'],

                            'cash_id' => $id,

                            'order_goods_id' => $v['id'],

                            'balance' => $v['fx_balance'],

                            'cash'    => $v['fx_cash'] ,

                            'single_cash'=> round($v['fx_cash']/$v['goods_num'],2),

                            'num'     => $v['goods_num'],
                        ];

                        $cash_goods_model->dataAdd($insert);

                    }

                }

            }


        }

        return true;
    }


    /**
     * @param $order
     * @param $type
     * @param int $status
     * @功能说明:添加土地订单分销
     * @author chenniang
     * @DataTime: 2022-07-27 13:53
     */
    public function addUserCashLand($fx_id,$top_id,$order,$type,$status=1){

        $land_model = new LandList();

        $land = $land_model->dataInfo(['id'=>$order['land_id']]);

        if(!empty($land['is_fx'])){

            $fx_balance = $type==1?$land['one_fx']:$land['two_fx'];

            $total_cash = $order['pay_price']*$fx_balance/100;

            $insert = [

                'uniacid'   => $order['uniacid'],

                'user_id'   => $top_id,

                'source_id' => $order['user_id'],

                'order_code'=> $order['order_code'],

                'transaction_id'=> $order['transaction_id'],

                'order_id' => $order['id'],

                'type'     => 2,

                'status'   => $status,

                'fx_type'  => $type,

                'reseller_id'=> $fx_id,

                'cash'     => round($total_cash,2),
            ];

            $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $insert = [

                'uniacid' => $order['uniacid'],

                'cash_id' => $id,

                'order_goods_id' => $order['id'],

                'balance' => $fx_balance,

                'cash'    => $total_cash ,

                'single_cash'=> $total_cash,

                'num'     => 1,
            ];

            $cash_goods_model = new DistributionGoods();

            $cash_goods_model->dataAdd($insert);

        }

        return true;
    }



    /**
     * @param $order
     * @param $type
     * @param int $status
     * @功能说明:添加认养订单分销
     * @author chenniang
     * @DataTime: 2022-07-27 13:53
     */
    public function addUserCashClaim($fx_id,$top_id,$order,$type,$status=1){

        $land_model = new Claim();

        $land = $land_model->dataInfo(['id'=>$order['claim_id']]);

        if(!empty($land['is_fx'])){

            $fx_balance = $type==1?$land['one_fx']:$land['two_fx'];

            $total_cash = $order['pay_price']*$fx_balance/100;

            $insert = [

                'uniacid'   => $order['uniacid'],

                'user_id'   => $top_id,

                'source_id' => $order['user_id'],

                'order_code'=> $order['order_code'],

                'transaction_id'=> $order['transaction_id'],

                'order_id' => $order['id'],

                'type'     => 3,

                'status'   => $status,

                'fx_type'  => $type,

                'reseller_id'  => $fx_id,

                'cash'     => round($total_cash,2),
            ];

            $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $insert = [

                'uniacid' => $order['uniacid'],

                'cash_id' => $id,

                'order_goods_id' => $order['id'],

                'balance' => $fx_balance,

                'cash'    => $total_cash ,

                'single_cash'=> round($total_cash/$order['num']),

                'num'     => $order['num'],
            ];

            $cash_goods_model = new DistributionGoods();

            $cash_goods_model->dataAdd($insert);

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-27 15:47
     * @功能说明:增加佣金信息
     */
    public function addUserCash($order,$type=1){

        $user_model = new User();

        $top_id = $user_model->where(['id'=>$order['user_id'],'is_fx'=>1])->value('pid');
        //没有上级不往下执行
        if(empty($top_id)){

            return true;
        }
        //二级分销
        if($type==2){

            $top_id = $user_model->where(['id'=>$top_id,'is_fx'=>1])->value('pid');

            if(empty($top_id)){

                return true;
            }

        }

        $fx_model = new DistributionList();

        $dis = [

            'user_id' => $top_id,

            'status'  => 2
        ];
        //查询分销id
        $fx_id = $fx_model->where($dis)->value('id');

        if(empty($fx_id)){

            return true;
        }

        if($type==1){
            //商城一级分销
            $this->addUserCashShop($fx_id,$top_id,$order,1);
            //商城二级分销
            $this->addUserCashShop($fx_id,$top_id,$order,2);

        }elseif ($type==2){
            //土地一级分销
            $this->addUserCashLand($fx_id,$top_id,$order,1);
            //土地二级分销
            $this->addUserCashLand($fx_id,$top_id,$order,2);

        }else{
            //土地一级分销
            $this->addUserCashClaim($fx_id,$top_id,$order,1);
            //土地二级分销
            $this->addUserCashClaim($fx_id,$top_id,$order,2);

        }
        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-28 17:18
     * @功能说明:佣金到账
     */
    public function cashArrival($order,$type=1){

        $dis = [

            'order_id' => $order['id'],

            'type'     => $type,

            'status'   => 1
        ];

        $list = $this->dataInfo($dis);

        if(!empty($list)){

            $res = $this->dataUpdate($dis,['status'=>2]);

            if($res==0){

                return false;
            }

            $user_model = new User();

            $cash = $list['cash'];

            $res = $user_model->where(['id'=>$order['user_id']])->update(['fx_cash'=>Db::Raw("fx_cash+$cash")]);

            if($res==0){

                return false;
            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-29 11:49
     * @功能说明:分销佣金列表
     */
    public function adminCashList($dis,$where,$page=10){

        $data = $this->alias('a')
                ->join('lbfarm_v2_distribution_list b','a.reseller_id = b.id','left')
                ->join('lbfarm_user_list c','a.user_id = c.id','left')
                ->where($dis)
                ->where(function ($query) use ($where){
                    $query->whereOr($where);
                })
                ->field('a.*,c.nickName,b.user_name')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }


}